﻿#pragma once

#include "common.h"
#include "ui/ui.h"
#include "ui/control_base.h"

namespace anip
{
	class Texture2D;

	namespace ui
	{
		class ButtonBase : public WithText<Control>
		{
		private:
			double _lastClick = 0.0;

		protected:
			bool _select = false;
			void processMouseEnter() override;
			void processMouseLeave() override;
			void processMouseButton(MouseButton btn, ButtonAction action) override;

		public:
			std::function<void(ButtonBase& self)>
				onClick,
				onDoubleClick;

			bcolor
				normalColor = r::Button_NormalColor,
				selectedColor = r::Button_SelectedColor;

			bfloat
				pressFactor = r::Button_PressFactor,
				hoverFactor = r::Button_HoverFactor;

			bool 
				enabled = true,
				toggleOnClick = false;

			virtual bool isSelected() const { return _select; }
			virtual void select(bool s = true) { _select = s; }

			ButtonBase(Container* parent = nullptr,
				invec2 pos = { 0, 0 }, invec2 size = { 0, 0 },
				const wstring& text = L"");

			void setSize(invec2 s) override;

			//void applyTheme(Theme& t) override;
		};

		class Button : public ButtonBase
		{
		protected:
			int getTextWidth() override;

		public:
			Button(Container* parent = nullptr,
				invec2 pos = { 0, 0 }, invec2 size = { 0, 0 },
				const wstring& text = L"");

			Texture2D* texture = nullptr;
			void paint() override;
			void flow(Flow& f) override;
		};

		class RoundButton : public ButtonBase
		{
		protected:
			int getTextWidth() override { return (int)(0.7f * _size.x); }

			bool isPointInside(invec2 p) override
			{
				if (!math::inRectangle(p, { 0, 0 }, _size)) return false;
				return powf(p.x - _size.x / 2.0f, 2) 
						+ powf(p.y - _size.x / 2.0f, 2) 
						<= powf(_size.x / 2.0f, 2);
			}

		public:
			RoundButton(Container* parent = nullptr,
				invec2 pos = { 0, 0 }, invec2 size = { 0, 0 },
				const wstring& text = L"");

			Texture2D* texture = nullptr;
			void paint() override;

			void setSize(invec2 s) override 
			{
				float w = std::min(s.x, s.y);
				Control::setSize({ w, w });
			}
		};
	}
}